/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package library.gui;

import java.io.File;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import library.gui.UserPanel;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JFileChooser;
import library.Item;
import library.Library;
import library.User;
import library.XmlWriter;
import library.XmlReader;

/**
 *
 * @author ryan
 */
public class LibraryFrame extends javax.swing.JFrame{
    
    private JMenuBar menuBar;
    private JMenu fileMenu;
    private JMenuItem loadItem;
    private JMenuItem saveItem;
    private UserPanel userPanel;
    private InventoryPanel inventoryPanel;
    private File libFile = null;
    Library lib;
    /** Creates new form LibraryFrame */
    public LibraryFrame(){
        lib= new Library();
        initComponents();       
    }
    
    private void initComponents(){
        
        //Set up the menu bar
        menuBar = new JMenuBar();
        fileMenu = new JMenu();
        fileMenu.setText("File");
        
        
        loadItem = new JMenuItem();
        fileMenu.add(loadItem);
        loadItem.setText("Load");
        loadItem.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent a) {
                try{
                    //Create a file chooser
                    JFileChooser fc = new JFileChooser();
                    if(JFileChooser.APPROVE_OPTION == fc.showOpenDialog(LibraryFrame.this))
                    {
                        libFile = fc.getSelectedFile();
                        lib = XmlReader.getLibrary(libFile);
                        LibraryFrame.this.getContentPane().removeAll();
                        LibraryFrame.this.initComponents();
                        LibraryFrame.this.repaint();
                    }
                }
                catch(Exception e)
                {
                   e.printStackTrace();
                   JOptionPane.showMessageDialog(null,"Could not open file.","Error", JOptionPane.ERROR_MESSAGE); 
                }
            }
        });
        
        
        
        saveItem = new JMenuItem();
        fileMenu.add(saveItem);
        saveItem.setText("Save");
        saveItem.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                if(libFile == null)
                {
                    //Create a file chooser
                    JFileChooser fc = new JFileChooser();
                    if(JFileChooser.APPROVE_OPTION == fc.showSaveDialog(LibraryFrame.this))
                    {
                        libFile = fc.getSelectedFile();
                    }
                }
                
                XmlWriter.saveLibrary(lib, libFile);
            }
        });
        

        menuBar.add(fileMenu);
        setJMenuBar(menuBar);
        
        //Set up the Frames main layout.  This is a horizontal layout
        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS));
        
        //Add the UserPanel
        userPanel = new UserPanel(lib);
        add(userPanel);
        
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new BoxLayout(buttonPanel,BoxLayout.Y_AXIS));
        JButton checkout = new JButton("<- Checkout");
        checkout.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                User u = userPanel.getSelectedUser();
                Item i = inventoryPanel.getSelectedItem();
                if(i != null && u != null)
                {
                    if(!lib.checkout(i, u)){
                        JOptionPane.showMessageDialog(null,"Constraint not met","Checkout error", JOptionPane.ERROR_MESSAGE);                   
                    }
                }
            }
        });
        
        buttonPanel.add(checkout);
        
        JButton checkin = new JButton("Checkin ->");
        buttonPanel.add(checkin);
        checkin.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                User u = userPanel.getSelectedUser();
                Item i = userPanel.getSelectedItem();
                if(i != null && u != null)
                    lib.checkin(u, i);
            }
        });
        
        add(buttonPanel);
        
        inventoryPanel = new InventoryPanel(lib);
        add(inventoryPanel);
        pack();
        setSize(1024,768);        
    }
    
     /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(LibraryFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(LibraryFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(LibraryFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(LibraryFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame main = new LibraryFrame();
                main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                main.setVisible(true);                
            }
        });
    }
    
    
}
